package com.family.gmall.web.controller;

import com.family.gmall.cart.client.CartFeignClient;
import com.family.gmall.model.product.entity.SkuInfo;
import com.family.gmall.product.client.ProductFeignClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: family
 * @Date: 2023/7/29
 * @Description:
 */
@Controller
public class CartController {
    @Resource
    private CartFeignClient cartFeignClient;

    @Resource
    private ProductFeignClient productFeignClient;

    /**
     * 查看购物车
     *
     * @param request
     * @return
     */
    @RequestMapping("cart.html")
    public String index(HttpServletRequest request) {
        return "cart/index";
    }

    @RequestMapping("addCart.html")
    public String addCart(
            @RequestParam(name = "skuId") Long skuId,
            @RequestParam(name = "skuNum") Integer skuNum,
            HttpServletRequest request) {
        cartFeignClient.addToCart(skuId, skuNum);
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        request.setAttribute("skuInfo", skuInfo);
        request.setAttribute("skuNum", skuNum);
        return "cart/addCart";
    }
}
